from django.db import models
from order.models import OrderRating

# Create your models here.

class EvaluationAnalysis(models.Model):
    """评价分析记录表"""
    order_rating = models.ForeignKey(OrderRating, on_delete=models.CASCADE, related_name='analyses')
    sentiment_score = models.FloatField(verbose_name="情感得分")  # -1到1之间，负数为负面，正数为正面
    sentiment_label = models.CharField(max_length=20, verbose_name="情感标签")  # 正面/负面/中性
    key_phrases = models.JSONField(verbose_name="关键短语")  # 提取的关键词和短语
    improvement_areas = models.JSONField(verbose_name="改进领域")  # 需要改进的方面
    analysis_time = models.DateTimeField(auto_now_add=True, verbose_name="分析时间")
    
    class Meta:
        db_table = 'evaluation_analysis'
        verbose_name = '评价分析记录'
        verbose_name_plural = '评价分析记录'

class ImprovementSuggestion(models.Model):
    """改进建议表"""
    analysis = models.ForeignKey(EvaluationAnalysis, on_delete=models.CASCADE, related_name='suggestions')
    suggestion_type = models.CharField(max_length=50, verbose_name="建议类型")  # 服务态度/驾驶技术/车辆状况等
    suggestion_content = models.TextField(verbose_name="建议内容")
    priority = models.SmallIntegerField(verbose_name="优先级")  # 1-高, 2-中, 3-低
    generated_time = models.DateTimeField(auto_now_add=True, verbose_name="生成时间")
    
    class Meta:
        db_table = 'improvement_suggestion'
        verbose_name = '改进建议'
        verbose_name_plural = '改进建议'

class TrainingRecommendation(models.Model):
    """培训建议表"""
    driver_id = models.BigIntegerField(verbose_name="司机ID")
    recommendation_type = models.CharField(max_length=50, verbose_name="培训类型")  # 服务礼仪/安全驾驶/车辆维护等
    recommendation_content = models.TextField(verbose_name="培训建议内容")
    training_materials = models.JSONField(verbose_name="培训材料")  # 相关培训材料链接或内容
    priority = models.SmallIntegerField(verbose_name="优先级")  # 1-高, 2-中, 3-低
    status = models.SmallIntegerField(default=0, verbose_name="状态")  # 0-未推送, 1-已推送, 2-已查看, 3-已完成
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    pushed_time = models.DateTimeField(null=True, blank=True, verbose_name="推送时间")
    
    class Meta:
        db_table = 'training_recommendation'
        verbose_name = '培训建议'
        verbose_name_plural = '培训建议'

class DriverImprovementMemory(models.Model):
    """司机改进记忆表 - 用于重构memory模式"""
    driver_id = models.BigIntegerField(verbose_name="司机ID")
    memory_type = models.CharField(max_length=50, verbose_name="记忆类型")  # 评价历史/改进记录/培训完成情况等
    memory_key = models.CharField(max_length=100, verbose_name="记忆键")
    memory_value = models.JSONField(verbose_name="记忆值")
    importance_score = models.FloatField(default=1.0, verbose_name="重要性得分")
    last_updated = models.DateTimeField(auto_now=True, verbose_name="最后更新时间")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        db_table = 'driver_improvement_memory'
        verbose_name = '司机改进记忆'
        verbose_name_plural = '司机改进记忆'
        unique_together = ['driver_id', 'memory_type', 'memory_key']
        indexes = [
            models.Index(fields=['driver_id', 'memory_type']),
            models.Index(fields=['importance_score']),
        ]
